﻿using System;
using System.Windows;

namespace Sinacor.OMM.Deployment.Setup
{
	public partial class PreRequisitos
    {
        #region [ Properties ]

        Conexao _conexao;

        #endregion

        #region [ Constructor ]

        public PreRequisitos()
		{
			this.InitializeComponent();
            this.ResizeMode = ResizeMode.NoResize;
        }

        #endregion

        #region [ Events ]

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CheckRequisites();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
            OpenNextStep();            
        }

        private void OpenNextStep()
        {
            if(_conexao == null)
                _conexao = new Conexao(this);
            _conexao.Show();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (SetupBase.ShowExitConfirm())
                SetupBase.CloseMainWindow();                   
            else
                e.Cancel = true;                         
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            if (SetupBase.ShowExitConfirm())
                SetupBase.CloseMainWindow();
        }

        #endregion

        #region [ Methods ]

        private void CheckRequisites()
        {
            Setup.Steps.CheckPreRequisites preRequisites = new Sinacor.OMM.Deployment.Setup.Steps.CheckPreRequisites();
            System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)delegate() { SetVisiblityControls(preRequisites.CheckOracleInstalled(),preRequisites.CheckMSMQInstalled()); }, null);                   
        }

        private void VisibilityAsyncCallBack(IAsyncResult ar)
        {
            Func<bool, bool, bool> visibilityDispatcher = (Func<bool,bool, bool>)ar.AsyncState;            
            visibilityDispatcher.EndInvoke(ar);
        }

        private void SetVisiblityControls(bool oracleInstalled, bool mSMQInstalled)
        {
            loader02.Visibility = Visibility.Hidden;

            if (oracleInstalled)
            {
                LedOracleOK.Visibility = Visibility.Visible;
                lblOKOracleClient.Visibility = Visibility.Visible;                
            }
            else
            {
                LedOracleNOK.Visibility = Visibility.Visible;
                lblNoOracleClient.Visibility = Visibility.Visible;
            }

            lblSearchComponentOracle.Visibility = Visibility.Hidden;            

            loader01.Visibility = Visibility.Hidden;

            if (mSMQInstalled)
            {
                LedMSMQOK.Visibility = Visibility.Visible;
                lblOKMessageQueue.Visibility = Visibility.Visible;
            }
            else
            {
                LedMSMQNOK.Visibility = Visibility.Visible;
                lblNoMessageQueue.Visibility = Visibility.Visible;
            }

            lblSearchComponentMQ.Visibility = Visibility.Hidden;

            if (oracleInstalled && mSMQInstalled)
                btnNext.Visibility = Visibility.Visible;            
        }        

        #endregion   
    }
}